# 单独运行下面这一行代码，由于并不存在bug.txt文件，所以无法打开
#open("D:/STUDY/python/异常、模块与包/bug.txt","r",encoding="UTF-8")


try:
    open("D:/STUDY/python/异常、模块与包/bug.txt", "r", encoding="UTF-8")
except:#直接使用except，捕获所有类型的异常
    print("出现异常，文件不存在无法打开，所以改用w模式打开")
    open("D:/STUDY/python/异常、模块与包/bug.txt", "w", encoding="UTF-8")

# 捕获指定异常
try:
    print(name)
except NameError as e:
    print("出现变量未定义的问题")
    print(e)

# 捕获多种异常
try:
    print(name1)
except (NameError,ZeroDivisionError) as x:
    print("出现了变量未定义问题 或者 除以0的异常")

# 捕获所有异常
# try:
# except Exception as a: